/*
 * Aca ocurre principalmente el display.
 *
 * This is part of the game The Rooms
 * Copyright (C) 2010  Elvio Sarquis
 *
 * Get the GNU GPL v3 in http://www.gnu.org/licenses/
 */

package rooms;

/**
 *
 * @author dask
 */
public class Render{
    
    private Screen buffer;
    private Screen bg;
    private Map map;
    private Character[] chars;
    private String[] msgs;
    private int state;
    private int inmenu;
    private int menuopt;

    public Render(){

        buffer = new Screen(64,25);
        map = null;
        chars = null;
        msgs = null;
        state = 0;
        menuopt = 0;
        inmenu = 0;
    }

    public void render(){

        if(state == C.sGAME && inmenu != C.imNORMAL) bg = buffer;

        buffer.cls();
        switch(state){

            case C.sMENU:

                switch(inmenu){

                    case 0:

                        renderMainMenu();
                }
                break;

            case C.sGAME:

                switch(inmenu){

                    case C.imNORMAL:

                        renderGame();
                        break;

                    case C.imDEAD:

                        renderGame();
                        renderGameOver();
                        break;
                }
                break;
        }
    }

    public void setMsgs(String[] msgs){
        this.msgs = msgs;
    }

    public void setMap(Map map){
        this.map = map;
    }

    public void setChars(Character[] chars){
        this.chars = chars;
    }

    public void setOption(int MenuOpt){
        menuopt = MenuOpt;
    }

    public void setState(int gamestate){
        state = gamestate;
    }

    public void setInMenu(int menu){
        inmenu = menu;
    }
    
    public Screen getBuffer(){
        return buffer;
    }

    public int getState(){
        return state;
    }

    public int getInMenu(){
        return inmenu;
    }

    public int getMenuOpt(){
        return menuopt;
    }

    private void renderGameOver(){

        for(int i = 0; i < buffer.getX();i++)
            for(int j = 0; j < buffer.getY();j++)
                buffer.setColor(i, j, C.LIGHT_GRAY);

        buffer.print(23, 3, "---------", C.WHITE);
        buffer.print(22, 4, "|Game Over|", C.WHITE);
        buffer.print(23, 5, "---------", C.WHITE);
    }

    private void renderGame(){
        
        displayMap();
        displayBar();
        displayMsg();
        for(int i= 0; i<25;i++)
            buffer.print(50, i, "|", C.GRAY);
        for(int i= 0; i<50;i++)
            buffer.print(i, 20, "-", C.GRAY);

        map=null;
        chars=null;
    }

    private void renderMainMenu(){

        buffer.cls();

        buffer.print(25,10,"The R.O.O.M.S.",C.RED);
        buffer.print(30,15,"Start",C.WHITE);
        buffer.print(30,16,"Load",C.WHITE);
        buffer.print(30,17,"Option",C.WHITE);
        buffer.print(30,18,"Exit",C.WHITE);
        buffer.print(25,24,"By Elvio Sarquis",C.DARK_RED);

        buffer.print(27,15 + menuopt,"->", C.BLUE);
    }
    
    private void displayMap(){
        
        char c='.';
        int pcx, pcy, CX=25, CY=10;

        pcx = chars[0].getX();
        pcy = chars[0].getY();

        for(int i=0; i<50;i++)
        for(int j=0; j<20;j++){

            switch(map.getMapPoint((pcx - CX) + i, (pcy - CY)+j)){
                case C.NO: c=' ';break;
                case C.FLOOR: c='·';break;
                case C.WALL: c='#';break;
                case C.CDOOR: c='+';break;
                case C.ODOOR: c='/';break;
                case C.DOWNS: c='>';break;
                case C.UPS: c='<';break;
                default: c='v';
            }

            buffer.print(i, j, c, C.WHITE);
        }

        buffer.print(25, 10, chars[0].getSimbol(), chars[0].getColor());

        for(int i = 1 ; i < chars.length ; i++ )
            buffer.print((chars[i].getX()+ CX - pcx), (chars[i].getY()+ CY - pcy), chars[i].getSimbol(), chars[i].getColor());
    }

    private void displayBar(){
        String s="";

        buffer.print(51, 0, chars[0].getName(), C.WHITE);
        buffer.print(51, 2, chars[0].getRace(), C.WHITE);
        buffer.print(51, 3, "Clase/Nivel", C.WHITE);
        buffer.print(51, 4, "XP/XP para", C.WHITE);

        buffer.print(51, 6, "|", C.WHITE);
        buffer.print(63, 6, "|", C.WHITE);
        buffer.print(52, 6, "///////////", C.RED);
        
        for(int x = 0; x < (chars[0].getHP()*11)/chars[0].getMaxHP(); x++)
            s = s + "/";
        
        buffer.print(52, 6, s, C.GREEN);
        buffer.print(51, 7, "HP: " + chars[0].getHP() + "/" +chars[0].getMaxHP(), C.WHITE);

        s = "";
        buffer.print(51, 9, "|", C.WHITE);
        buffer.print(63, 9, "|", C.WHITE);
        buffer.print(52, 9, "///////////", C.RED);
        
        for(int x = 0; x < (chars[0].getMana()*11)/chars[0].getMaxMana(); x++)
            s = s + "/";
        
        buffer.print(52, 9, s, C.BLUE);
        buffer.print(51, 10, "Mana: " + chars[0].getMana() + "/" +chars[0].getMaxMana(), C.WHITE);

        buffer.print(51, 12, "STR: " + chars[0].getBaseStr(), C.WHITE);
        buffer.print(51, 13, "DEX: " + chars[0].getBaseDex(), C.WHITE);
        buffer.print(51, 14, "INT: " + chars[0].getBaseWis(), C.WHITE);
    }

    private void displayMsg(){

        //Barra de eventos, abajo
        buffer.print(1, 21, "Dungeon of Baddnes                         Lvl-1", C.GRAY);

        if(msgs.length>0)
            buffer.print(0,22,msgs[0], C.WHITE);
        if(msgs.length>1)
            buffer.print(0,23,msgs[1], C.WHITE);
        if(msgs.length>2)
            buffer.print(0,24,msgs[2], C.WHITE);
    }
}
